package cn.edu.nju.software.cdiy.action.user.all;

import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.cdiy.bean.Hardware;
import cn.edu.nju.software.cdiy.bean.Hcomment;
import cn.edu.nju.software.cdiy.bean.Scheme;
import cn.edu.nju.software.cdiy.service.HcommentService;
import cn.edu.nju.software.cdiy.service.Service;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class HardwareOverviewAction<T extends Hardware> extends ActionSupport implements SessionAware {

	private static String S_HARDWARE;
	private static String HARDWARE_TYPE;
	private Map<String, Object> session;
	private Service<T> service;
	private HcommentService hcommentService;
	private int id;
	private Hcomment newUserComment;
	private Hcomment newExpertComment;
	private List<Scheme> relatedSchemes;
	private int userCommentAmount;
	private int expertCommentAmount;

	public static void setS_HARDWARE(String s_HARDWARE) {
		S_HARDWARE = s_HARDWARE;
	}

	public static void setHARDWARE_TYPE(String hARDWARE_TYPE) {
		HARDWARE_TYPE = hARDWARE_TYPE;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setService(Service<T> service) {
		this.service = service;
	}

	public List<Scheme> getRelatedSchemes() {
		return relatedSchemes;
	}

	public void setHcommentService(HcommentService hcommentService) {
		this.hcommentService = hcommentService;
	}

	public void setId(int id) {
		this.id = id;
	}

	public Hcomment getNewUserComment() {
		return newUserComment;
	}

	public Hcomment getNewExpertComment() {
		return newExpertComment;
	}

	public int getUserCommentAmount() {
		return userCommentAmount;
	}

	public int getExpertCommentAmount() {
		return expertCommentAmount;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		T sessionHardware = (T) session.get(S_HARDWARE);
		if (sessionHardware==null || sessionHardware.getId()!=id) {
			T hardware = service.getOneById(id);
			if (hardware == null) {
				return ERROR;
			}
			session.put(S_HARDWARE, hardware);
		}
		relatedSchemes = service.getRelatedSchemes(id);
		newUserComment = hcommentService.getNewUserHComment(id, HARDWARE_TYPE);
		newExpertComment = hcommentService.getNewExpertHComment(id, HARDWARE_TYPE);
		userCommentAmount = hcommentService.getUserHCommentLength(id, HARDWARE_TYPE);
		expertCommentAmount = hcommentService.getExpertHCommentLength(id, HARDWARE_TYPE);
		
		return SUCCESS;
	}

}
